package com.bub.pay.mq.config;

import java.util.Properties;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.bean.TransactionProducerBean;
import com.bub.pay.mq.rocketmq.transactioncheck.BNLocalTransactionChecker;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties({ MqProperties.class, TopsProperties.class })
public class MqConfig {

	@Resource
	private MqProperties mqProperties;

	@Bean(initMethod = "start", destroyMethod = "shutdown")
	public ProducerBean getProducerBean() {
		ProducerBean producerBean = new ProducerBean();
		Properties properties = new Properties();
		properties.setProperty("AccessKey", mqProperties.getAccessKey());
		properties.setProperty("SecretKey", mqProperties.getSecretKey());
		properties.setProperty("NAMESRV_ADDR", mqProperties.getNamesrvAddr());
		producerBean.setProperties(properties);
		producerBean.start();
		return producerBean;
	}

	@Bean
	public BNLocalTransactionChecker localTransactionChecker() {
		return new BNLocalTransactionChecker();
	}

	
	@Bean(initMethod = "start", destroyMethod = "shutdown")
	public TransactionProducerBean getTransactionProducerBean() {
		TransactionProducerBean producerBean = new TransactionProducerBean();
		Properties properties = new Properties();
		properties.setProperty("AccessKey", mqProperties.getAccessKey());
		properties.setProperty("SecretKey", mqProperties.getSecretKey());
		properties.setProperty("NAMESRV_ADDR", mqProperties.getNamesrvAddr());
		properties.setProperty("GROUP_ID", mqProperties.getGroupId());
		producerBean.setProperties(properties);
		producerBean.setLocalTransactionChecker(localTransactionChecker());
		return producerBean;
	}
	
}
